﻿#include "com_manager.h"
#include "dialog_ethernet_setting.h"
#include "plug_info.h"
#include "widget/tool_tipper.h"
#include "ui_dialog_ethernet_setting.h"

#include <QRegExpValidator>
#include <nuf_bus_info.h>



#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif




DialogEthernetSetting::DialogEthernetSetting(int id, int node,  QWidget* parent)
	: QDialog(parent)
    , ui(new Ui::DialogNetSetting)
     , node_id(node)
{
	ui->setupUi(this);

    ethernet_id = QString("ethernet_%1").arg(id, 2,10, QLatin1Char('0'));
    QString node_tetx = QString("node%1_p%2").arg(node).arg( id ) ;
    ui->label_id->setText(node_tetx);

   // QString node_name = QString("board_%1").arg(node, 2, 10, QLatin1Char('0'))
    if (NufBusSetting::map_bus.count() >= node )
        ui->widget_ip->setIP(NufBusSetting::map_bus[node-1].mac_id.mid(3, 11) );

  //  ui->label_id->installEventFilter(new ToolTipper(ui->label_id));

    this->setWindowTitle("以太网设置："  );

    //  ui->widget_ip     ->installEventFilter(new ToolTipper( ui->widget_ip));

  //  ui->lineEdit_mac->setInputMask("00-00-00-00");
  //  ui->lineEdit_mac->setValidator(new QRegExpValidator (
  //      QRegExp(QString("/^[A-F0-9]{2}(-[A-F0-9]{2}){5}$|^[A-F0-9]{2}(:[A-F0-9]{2}){5}$/"))) );
   // ui->lineEdit_mac->setPlaceholderText(QString("00-00-00-00"));

   // ui->label_9->installEventFilter(new ToolTipper( ui->lineEdit_mac));

	// 正在表达式限制输入
	//ui->lineEdit_ip->setValidator(new QRegExpValidator(QRegExp("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b")));
	// 用于占位
	//ui->lineEdit_ip->setInputMask("000.000.000.000; ");
}

DialogEthernetSetting::~DialogEthernetSetting()
{
	delete ui;
}

void DialogEthernetSetting::on_button_ok_clicked()
{
	_ETHERNET ethernet;
	//ethernet.ip = ui->lineEdit_ip->text();
	ethernet.ip = ui->widget_ip->getIP();
    
    PlugInfo::map_ethernet[ethernet_id] = ethernet;

    //NodeSetting::saveNodeSetting();

    QString new_mad_id =  "86-" + ethernet.ip + "-03";


    rs232com * rs232 = com_manager::instance()->get_com(NufBusSetting::map_bus[node_id].port_name);

    NufBusSetting::map_bus[node_id-1].mac_id = new_mad_id;

    rs232->set_mac_address(new_mad_id.remove('-'));


    //NodeSetting::saveBusSetting() ;

	accept();
}


void DialogEthernetSetting::on_button_cancel_clicked()
{
    _ETHERNET ethernet;
    //ethernet.ip = ui->lineEdit_ip->text();
    ethernet.ip = ui->widget_ip->getIP();
    
    PlugInfo::map_ethernet[ethernet_id] = ethernet;
    
    PlugInfo::save_port_Setting();

	reject();
}
